package com.link2room.blaster.facade.impl;

import com.link2room.blaster.facade.IInmailTypeFacadeService;
import com.link2room.common.constant.DefaultValue;
import com.link2room.common.dto.param.base.BaseL2rParamDto;
import com.link2room.common.exception.SysException;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.L2RAssert;
import com.link2room.blaster.dto.inMail.InmailTypeDto;
import com.link2room.blaster.dto.param.inMail.InmailTypeParamDto;
import com.link2room.blaster.entity.InMailTypeEntity;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static com.link2room.enums.exception.BlasterSysExceptionEnum.INMAIL_TYPE_ERROR;


/**
 * Created by LN on 2017/6/28.16:00
 */
@Component("inmailTypeFacadeService")
public class InmailTypeFacadeServiceImpl implements IInmailTypeFacadeService {
    @Resource
    private ICommonService commonService;
    @Override
    public void createNewInmailType(InmailTypeParamDto typeParamDto) throws Exception {
        String lhotelGroupCode = typeParamDto.getLhotelGroupCode();
        L2RAssert.stringBlank(lhotelGroupCode,"[lhotelGroupCode] is null.");
        String code = typeParamDto.getCode();
        L2RAssert.stringBlank(code,"[code] is null.");
        String description = typeParamDto.getDescription();
        L2RAssert.stringBlank(description,"[description] is null.");
        String name = typeParamDto.getName();
        L2RAssert.stringBlank(name,"[name] is null.");
        L2RAssert.isTrue(hasType(code),new SysException(INMAIL_TYPE_ERROR,"[code] is  duplicate."));
        L2RAssert.isTrue(hasName(code),new SysException(INMAIL_TYPE_ERROR,"[name] is  duplicate."));

        InMailTypeEntity newEntity = new InMailTypeEntity();
        newEntity.setName(name);
        newEntity.setCode(code);
        newEntity.setLhotelGroupCode(DefaultValue.LHOTEL_GROUP_CODE);
        commonService.save(newEntity);
    }

    @Override
    public List<InmailTypeDto> typeList(BaseL2rParamDto paramDto) throws Exception {
        Integer currentPage = paramDto.getCurrentPage();
        Integer pageSize = paramDto.getPageSize();
        InMailTypeEntity find = new InMailTypeEntity();
        find.setLhotelGroupCode(DefaultValue.LHOTEL_GROUP_CODE);
        find.setIsHalt(DefaultValue.F);
        QueryResult<InMailTypeEntity> qres = commonService.findAllEQ(InMailTypeEntity.class, find, currentPage, pageSize);
        if(qres != null && CollectionUtils.isNotEmpty(qres.getResultlist())){
            return qres.getResultlist().stream()
                    .map(entity-> ClassConverUtil.copyProperties(entity,InmailTypeDto.class))
                    .collect(Collectors.toList());
        }else {
            return Collections.emptyList();
        }
    }

    @Override
    public void update(InmailTypeParamDto typeParamDto) throws Exception {
        String code = typeParamDto.getCode();
        L2RAssert.stringBlank(code,"[code] is null.");
        L2RAssert.isTrue(!hasType(code),new SysException(INMAIL_TYPE_ERROR,code +" is not exit."));
        String name = typeParamDto.getName();
        L2RAssert.stringBlank(name , "[name] is null.");
        InMailTypeEntity findCount = new InMailTypeEntity();
        findCount.setName(name);
        long count = commonService.getCountAllEQ(InMailTypeEntity.class, findCount, null);
        boolean duplicate =  count > 1;
        L2RAssert.isTrue(duplicate,new SysException(INMAIL_TYPE_ERROR,name +" is duplicate."));
        String isHalt = typeParamDto.getIsHalt();
        L2RAssert.ishaltVaild(isHalt);
        InMailTypeEntity find = new InMailTypeEntity();
        find.setCode(code);
        InMailTypeEntity update = commonService.findOneEQ(InMailTypeEntity.class, find);
        update.setIsHalt(isHalt);
        update.setName(name);
        update.setDescription(typeParamDto.getDescription());
        commonService.updateById(update);
    }

    @Override
    public InmailTypeDto type(InmailTypeParamDto paramDto) throws Exception {
        String code = paramDto.getCode();
        L2RAssert.stringBlank(code,"[code] is null.");
        InMailTypeEntity find = new InMailTypeEntity();
        find.setCode(code);
        InMailTypeEntity qres = commonService.findOneEQ(InMailTypeEntity.class, find);
        if(qres!= null){
            return ClassConverUtil.copyProperties(qres,InmailTypeDto.class);
        }else{
            return null;
        }
    }

    private boolean hasType(String typeCode) throws Exception {
        InMailTypeEntity find = new InMailTypeEntity();
        find.setCode(typeCode);
        long count = commonService.getCountAllEQ(InMailTypeEntity.class, find, null);
        return count > 0;
    }

    private boolean hasName(String name) throws Exception {
        InMailTypeEntity find = new InMailTypeEntity();
        find.setName(name);
        long count = commonService.getCountAllEQ(InMailTypeEntity.class, find, null);
        return count > 0;
    }



}
